using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BerkoRules.Objects;
using Microsoft.VisualBasic.CompilerServices;
using Reportes;
using BerkoRules.Mappers;
using BerkoRules.Entities;

namespace Berko
{
    public partial class frmRpt_X_Historial : Form
    {
        private ClientesList _clientes;
        private int _index;

        public frmRpt_X_Historial()
        {
            InitializeComponent();
        }

        private DataTable Buscar_Datos()
        {
            ClientesList all = null;
            DataRow row;
            int num;
            int num2;
            Cadenas.Cadenas cadenas = new Cadenas.Cadenas();
            DataTable table2 = null;
            DataSet set = new DataSet();
            DataTable table3 = new DataTable();
            table3.Columns.Add("cliente");
            table3.Columns.Add("fecha");
            table3.Columns.Add("historial");
            try
            {
                all = ClientesMapper.Instance().GetAll();
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show("No se pudieron traer los clientes. Reintente por favor...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                DataTable table = table3;
                ProjectData.ClearProjectError();
                return table;
            }
            if (this.chkTodos.Checked)
            {
                if (all != null)
                {
                    int num3 = all.Count - 1;
                    for (num = 0; num <= num3; num++)
                    {
                        if (all[num].cli_Historial.Trim() != string.Empty)
                        {
                            table2 = cadenas.Armar_Cadenas(all[num].cli_Historial, Conversions.ToDate(this.txtDesde.Text), Conversions.ToDate(this.txtHasta.Text));
                            int num4 = table2.Rows.Count - 1;
                            num2 = 0;
                            while (num2 <= num4)
                            {
                                row = table3.NewRow();
                                row[0] = all[num].cli_Nombre;
                                row[1] = table2.Rows[num2][0];
                                row[2] = table2.Rows[num2][1];
                                table3.Rows.Add(row);
                                num2++;
                            }
                        }
                    }
                }
                return table3;
            }
            if (all != null)
            {
                int num5 = all.Count - 1;
                for (num = 0; num <= num5; num++)
                {
                    if (all[num].cli_ID == Conversions.ToInteger(this.cboClientes.SelectedValue))
                    {
                        if (all[num].cli_Historial.Trim() != string.Empty)
                        {
                            table2 = cadenas.Armar_Cadenas(all[num].cli_Historial, Conversions.ToDate(this.txtDesde.Text), Conversions.ToDate(this.txtHasta.Text));
                            int num6 = table2.Rows.Count - 1;
                            for (num2 = 0; num2 <= num6; num2++)
                            {
                                row = table3.NewRow();
                                row[0] = all[num].cli_Nombre;
                                row[1] = table2.Rows[num2][0];
                                row[2] = table2.Rows[num2][1];
                                table3.Rows.Add(row);
                            }
                        }
                        return table3;
                    }
                }
            }
            return table3;
        }

        private void chkTodos_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkTodos.Checked)
            {
                this.cboClientes.Enabled = false;
                this.cmdBuscar1.Enabled = false;
            }
            else
            {
                this.cboClientes.Enabled = true;
                this.cmdBuscar1.Enabled = true;
            }
        }

        private void cmdBuscar1_Click(object sender, EventArgs e)
        {
            if (this.cboClientes.Text.Trim() == string.Empty)
            {
                this._index = -1;
                MessageBox.Show("El cliente no existe", "Cliente inexistente", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.cboClientes.Text = string.Empty;
                this.cmdReporte.Enabled = false;
            }
            else
            {
                this._index = -1;
                int num2 = this._clientes.Count - 1;
                for (int i = 0; i <= num2; i++)
                {
                    if (this._clientes[i].cli_Nombre == this.cboClientes.Text)
                    {
                        this._index = i;
                        this.cboClientes.Text = this._clientes[i].cli_Nombre;
                        this.cmdReporte.Enabled = true;
                        this.cmdReporte.Focus();
                        break;
                    }
                }
                if (this._index == -1)
                {
                    MessageBox.Show("El cliente no existe", "Cliente inexistente", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.cboClientes.Text = string.Empty;
                    this.cmdReporte.Enabled = false;
                }
            }
        }

        private void cmdReporte_Click(object sender, EventArgs e)
        {
            this.Ver_reporte();
        }


        private void frmRpt_X_Historial_Load(object sender, EventArgs e)
        {
            this.Traer_Clientes();
            this.txtDesde.Focus();
        }


        private void Traer_Clientes()
        {
            try
            {
                this._clientes = ClientesMapper.Instance().GetAll();
                this.cboClientes.DataSource = this._clientes;
                this.cboClientes.DisplayMember = "cli_Nombre";
                this.cboClientes.ValueMember = "cli_ID";
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show("No se pudieron traer los clientes", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                ProjectData.ClearProjectError();
            }
        }

        private void Ver_reporte()
        {
            this.Cursor = Cursors.WaitCursor;
            this.cmdBuscar1.Enabled = false;
            this.cmdBuscar1.Refresh();
            rpt_PorHistorial historial = new rpt_PorHistorial();
            DataSet dataSet = new DataSet();
            DataTable table = new DataTable("dt_PorHistorial");
            if (this.cboClientes.SelectedIndex < 0)
            {
                MessageBox.Show("Seleccione un cliente v\x00e1lido", "Cliente inexistente", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                this.cboClientes.Focus();
                this.Cursor = Cursors.Default;
                this.cmdBuscar1.Enabled = true;
            }
            else
            {
                try
                {
                    table = this.Buscar_Datos();
                    table.TableName = "dt_PorHistorial";
                    dataSet.Tables.Add(table.Copy());
                }
                catch (Exception exception1)
                {
                    ProjectData.SetProjectError(exception1);
                    Exception exception = exception1;
                    this.Cursor = Cursors.Default;
                    this.cmdBuscar1.Enabled = true;
                    ProjectData.ClearProjectError();
                    return;
                    ProjectData.ClearProjectError();
                }
                historial.SetDataSource(dataSet);
                this.rptVisor.ReportSource = historial;
                this.rptVisor.RefreshReport();
                this.Cursor = Cursors.Default;
                this.cmdBuscar1.Enabled = true;
            }
        }



    }
}